<?php

include_once 'modulo.php';

class Model {

	var $db = null;
	var $user = null;
	var $pass = null;

	public function __construct($host,$dbname , $user , $pass){
		try {
			$this->db = new PDO('mysql:host='.$host.';dbname='.$dbname, $user, $pass,
				array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''));
			$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	public function checkUsedCode($object){
		try {
			$sql = "SELECT count(*)  FROM modulo where codice=?";
			$stmt = $this->db->prepare($sql);
			$stmt->bindParam(1, $object->codice);
			$stmt->execute();
			$count=0;
			if( $row = $stmt->fetch( PDO::FETCH_BOTH ) ) {
				$count=$row[0];
			}
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
		if($count>0) return TRUE;
		else return FALSE;
	}
	
	// TODO generalizzare tabella oggetto
	public function insertUpdate($object){
		try {
			$sql ="INSERT INTO modulo ( ";
			foreach ($object as $k => $v) {
				if($k=='pkid'||$k=='ts_ins')
				continue;
				$sql .= " ".$k.",";
			}
			$sql = substr($sql,0,-1);
			$sql .=" )  VALUES ( ";
			foreach ($object as $k => $v) {
				if($k=='pkid'||$k=='ts_ins')
				continue;
				$sql .=" :".$k.",";
			}
			$sql = substr($sql,0,-1);
			$sql .=" ) ";
			//print $sql;
			// prepared statement
			$stm = $this->db->prepare($sql);
			// bind values
			foreach ($object as $k => $v) {
				if($k=='pkid'||$k=='ts_ins')
				continue;
				if(is_numeric($v)){
					$stm->bindParam(':'.$k,intval($v),PDO::PARAM_INT);
				}else{
					$stm->bindParam(':'.$k, strval($v),PDO::PARAM_STR);
				}
			}
			$stm->execute();
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
		return $this->db->lastInsertId();
	}

	public function read($keys){
		// TODO
	}

	public function close(){
		$this->db=null;
	}

	public function exportCsv(){
		try {
			$sql ="select * from modulo";
			$quest = new modulo();
			$labels = $quest->getLabels();
			$result = $this->db->query($sql);
			$line  = "";
			foreach ($labels as $k =>$v) {
				$line .= $v.";";
			}
			print $line."\n";
			foreach ($result->fetchAll(PDO::FETCH_CLASS,"modulo") as $row) {
				$line  = "";
				foreach ($row as $k =>$v){
					$line .= $v.";";
				}
				$line = substr($line,0,-1);
				print $line."\n";
			}
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	public function db2xslt($pkid,$xslt){
		try {
			$sql ="SELECT * FROM modulo WHERE pkid=$pkid";
			$rs = $this->db->query($sql);
			if( $row = $rs->fetch( PDO::FETCH_ASSOC ) ) {
				$input =  $this->encode($row,"scheda_informativa",2);
				return $this->transform($input, $xslt);
			}
		} catch (PDOException $e) {
			print "Error!: " . $e->getMessage() . "<br/>";
			die();
		}
	}
	
	private function encode($data, $node, $depth) {
		$xml="";
		$xml .= str_repeat("\t", $depth);
		$xml .= "<$node>\n";
		foreach($data as $key => $val) {
			if(is_array($val) || is_object($val)) {
				$xml .= self::encode($val, $key, ($depth + 1));
			} else {
				$xml .= str_repeat("\t", ($depth + 1));
				$xml .= "<$key>" . htmlspecialchars($val) . "</$key>\n";
			}
		}
		$xml .= str_repeat("\t", $depth);
		$xml .= "</$node>\n";
		return $xml;
	}
	
	private function transform($xml, $xsltfile) {
		$xslt = new XSLTProcessor();
		$xsl = new DOMDocument();
		$xsl->load( $xsltfile, LIBXML_NOCDATA);
		$xslt->importStylesheet($xsl);
		return $xslt->transformToXml(new SimpleXMLElement($xml));
	}
}

?>